package com.bsoft.providertest;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;

import com.bsoft.factoryprovider.Goods;
import com.bsoft.factoryprovider.GoodsProvider;

import java.util.LinkedList;

/**
 * Created by 泅渡者
 * Created on 2017/9/28.
 * 数据库助手类
 */

public class FruitsHelper {

    private ContentResolver resolver = null;
    private GoodsProvider goodsProvider;

    public FruitsHelper(Context context) {
        resolver = context.getContentResolver();
        goodsProvider = new GoodsProvider();
    }

    /**
     * 向ContentProvider添加数据 并返回ID
     **/
    public long insertFruits(Fruits fruits) {

        ContentValues values = new ContentValues();
        values.put(Goods.NAME, fruits.name);
        values.put(Goods.DESC, fruits.desc);
        values.put(Goods.URL, fruits.url);

        Uri uri = resolver.insert(Goods.CONTENT_URI, values);
        String itemId = uri.getPathSegments().get(1);
        return Integer.valueOf(itemId).longValue();
    }

    /**
     * 对ContentProvider更新数据并 返回状态
     **/
    public boolean updateFruits(Fruits fruits) {
        Uri uri = ContentUris.withAppendedId(Goods.CONTENT_URI, fruits.id);

        ContentValues values = new ContentValues();
        values.put(Goods.NAME, fruits.name);
        values.put(Goods.DESC, fruits.desc);
        values.put(Goods.URL, fruits.url);

        int count = resolver.update(uri, values, null, null);
        return count > 0;
    }

    public boolean removeFruits(int id) {
        Uri uri = ContentUris.withAppendedId(Goods.CONTENT_URI, id);

        int count = resolver.delete(uri, null, null);

        return count > 0;
    }

    /**
     * 对Contentprovider的Fruits 表进行遍历
     **/
    public LinkedList<Fruits> getAllFruits() {
        LinkedList<Fruits> fruitses = new LinkedList<Fruits>();

        String[] projection = new String[]{
                Goods.ID,
                Goods.NAME,
                Goods.DESC,
                Goods.URL
        };

        Cursor cursor = resolver.query(Goods.CONTENT_URI, projection, null, null, Goods.DEFAULT_SORT_ORDER);
        if (cursor.moveToFirst()) {
            do {
                int id = cursor.getInt(0);
                String name = cursor.getString(1);
                String desc = cursor.getString(2);
                String url = cursor.getString(3);

                Fruits fruits = new Fruits(id, name, desc, url);
                fruitses.add(fruits);
            } while (cursor.moveToNext());
        }

        return fruitses;
    }

    public int getFruitsCount() {
        int count = 0;
        Bundle bundle = resolver.call(Goods.CONTENT_URI, Goods.METHOD_GET_ITEM_COUNT, null, null);
        count = bundle.getInt(Goods.KEY_ITEM_COUNT, 0);

        return count;
    }

    public Fruits getArticleById(int id) {
        Uri uri = ContentUris.withAppendedId(Goods.CONTENT_URI, id);

        String[] projection = new String[]{
                Goods.ID,
                Goods.NAME,
                Goods.DESC,
                Goods.URL
        };

        Cursor cursor = resolver.query(uri, projection, null, null, Goods.DEFAULT_SORT_ORDER);

        if (!cursor.moveToFirst()) {
            return null;
        }

        String title = cursor.getString(1);
        String abs = cursor.getString(2);
        String url = cursor.getString(3);

        return new Fruits(id, title, abs, url);
    }

    public Fruits getArticleByPos(int pos) {
        Uri uri = ContentUris.withAppendedId(Goods.CONTENT_POS_URI, pos);

        String[] projection = new String[]{
                Goods.ID,
                Goods.NAME,
                Goods.DESC,
                Goods.URL
        };

        Cursor cursor = resolver.query(uri, projection, null, null, Goods.DEFAULT_SORT_ORDER);
        if (!cursor.moveToFirst()) {
            return null;
        }

        int id = cursor.getInt(0);
        String title = cursor.getString(1);
        String abs = cursor.getString(2);
        String url = cursor.getString(3);

        return new Fruits(id, title, abs, url);
    }
}
